{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e05b692",
   "metadata": {},
   "source": [
    "# 1开箱即用的pipelines\n",
    "## 1.1情感分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "23167da0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\develop\\python_code\\llm_algorithm\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision 714eb0f (https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'label': 'POSITIVE', 'score': 0.9998855590820312}]\n",
      "[{'label': 'POSITIVE', 'score': 0.9998855590820312}, {'label': 'NEGATIVE', 'score': 0.9997503161430359}, {'label': 'NEGATIVE', 'score': 0.9987688660621643}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "sentiment_pipeline = pipeline(\"sentiment-analysis\")\n",
    "result = sentiment_pipeline(\"I love this product!\")\n",
    "print(result)\n",
    "\n",
    "results = sentiment_pipeline([\"I love this product!\", \"I hate this product!\", \"I feel neutral about this product.\"])\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "317820f6",
   "metadata": {},
   "source": [
    "## 1.2 零样本分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4dd6ea43",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to facebook/bart-large-mnli and revision d7645e1 (https://huggingface.co/facebook/bart-large-mnli).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sequence': 'This is a review about a product.I like it very much', 'labels': ['positive', 'neutral', 'negative'], 'scores': [0.9834312200546265, 0.01366201788187027, 0.002906710607931018]}\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline(\"zero-shot-classification\")\n",
    "result = classifier(\"This is a review about a product.I like it very much\", candidate_labels=[\"positive\", \"negative\", \"neutral\"])\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1d82846",
   "metadata": {},
   "source": [
    "## 1.3 文本生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9b16c10f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n",
      "Device set to use cpu\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
      "Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
      "Both `max_new_tokens` (=256) and `max_length`(=50) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'generated_text': 'in this course, we will teach you how to use the ‖singer‖ in the head.‖\\n\\n\\n\\nThe Head is a tool that provides a way to get rid of the ‖singer‖ in the head.\\nIn the head, you will need:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\nA:\\n'}]\n",
      "[{'generated_text': 'in this course, we will teach you how to make your own coffee. This course is designed to get you started with coffee.\\n\\n\\n\\nTo read more about the course, click here.'}, {'generated_text': 'in this course, we will teach you how to use the tools that you have already been using.\\n\\n\\n\\nThe next time you go through a class, make sure you have one of the tools available.\\nThis course is a step in the right direction. Once you have a class, you will be able to use the tools you have already used.\\nThe following classes will help you get started with this course.\\nThis class will allow you to use the tools you have already used. The tools you have already used will be available. The tools you have already used will be available.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe following classes will help you get started with this course.\\nThe'}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "generator = pipeline(\"text-generation\", model=\"distilgpt2\")\n",
    "# 第一次生成文本\n",
    "results = generator(\"in this course, we will teach you how to\")\n",
    "print(results)\n",
    "\n",
    "# 再次生成文本\n",
    "results = generator(\n",
    "    \"in this course, we will teach you how to\",\n",
    "    num_return_sequences=2,\n",
    "    max_length=50\n",
    ")\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c9db197c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n",
      "Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n",
      "Device set to use cpu\n",
      "Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.\n",
      "Both `max_new_tokens` (=256) and `max_length`(=40) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'generated_text': '[CLS] 千 锤 万 凿 出 深 山 ， 人 不 可 寻 。 惟 有 龟 山 水 ， 不 能 平 胸 襟 。 自 从 明 月 来 ， 得 此 光 满 林 。 载 一 壶 酒 ， 高 咏 涤 烦 襟 。 此 乐 不 可 说 ， 空 有 山 水 音 。 予 不 能 歌 ， 而 汝 意 难 任 。 见 山 海 间 ， 无 复 梦 寐 寻 。 人 独 何 幸 ， 白 发 还 相 侵 。 会 合 不 可 常 ， 虚 舟 以 沈 吟 。 哉 此 二 物 ， 难 处 不 可 寻 。 哉 百 年 间 遇 一 ， 可 见 。 不 逢 ， 我 不 可 见 ， 此 二 ， 使 自 虚 生 ， 不 应 无 。 。 我 自 不 可 见 ， 不 可 以 为 之 ， 不 可 。 劝 ， 焉 不 可 为 无 难 。 所 不 可 求 ， 岂 能 ， 将 两 ， 不 堪 。 道 不 可 。 子 万 物 。 一 无 可 以 。 ， 无 之 无 。 不 可 使 万 物 之 之 ， 无 长 焉 天 之 一 ， 我 心 之 万 物 之 不 可 为 其 一 ， 天 下 。 。 可 为 ， 天 下 之 百 ， 我 ， 天 之 天 一 之 。 两 ， 无 之 有 。'}, {'generated_text': '[CLS] 千 锤 万 凿 出 深 山 ， 忽 龙 吟 。 天 门 十 二 重 ， 四 壁 悬 千 寻 。 壮 哉 造 化 力 ， 不 尽 生 死 心 。 游 吾 之 初 ， 所 性 本 至 静 。 一 为 利 所 驱 ， 一 为 利 所 竞 。 今 我 与 世 违 ， 身 世 两 相 忘 。 既 来 亦 何 补 ， 而 得 亦 何 幸 。 忘 机 有 白 鸥 ， 照 影 时 自 省 。 笑 杨 道 人 ， 乃 独 忘 形 影 。 荷 君 子 德 ， 欲 报 君 子 恩 。 以 此 获 大 观 民 乐 郊 岛 夷 ， 且 化 兮 天 地 ， 天 地 ， 天 地 为 藩 。 人 。 东 海 海 为 池 ， 东 海 水 之 。 海 为 池 。 海 之 有 池 ， 西 焉 山 之 薮 ， 万 山 之 池 之 池 之 海 之 池 之 水 之 池 之 水 之 水 之 薮 之 海 之 不 水 兮 山 之 为 池 之 有 水 之 海 之 水 之 海 之 薮 。 山 之 薮 。 水 之 云 洋 洋 ， 九 山 之 云 山 之 木 ， 其 池 之 海 之 水 之 水 之 海 之 薮 之 水 之 水 之 水 之 水 之 薮 之 薮 之 海 之 水 之 大'}]\n"
     ]
    }
   ],
   "source": [
    "generator = pipeline(\"text-generation\", model=\"uer/gpt2-chinese-poem\")\n",
    "results = generator(\n",
    "    \"[CLS] 千 锤 万 凿 出 深 山 ，\",\n",
    "    max_length = 40,\n",
    "    num_return_sequences=2\n",
    ")\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "641709f5",
   "metadata": {},
   "source": [
    "## 1.4 遮盖词填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f6c61916",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to distilbert/distilroberta-base and revision fb53ab8 (https://huggingface.co/distilbert/distilroberta-base).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n",
      "Some weights of the model checkpoint at distilbert/distilroberta-base were not used when initializing RobertaForMaskedLM: ['roberta.pooler.dense.bias', 'roberta.pooler.dense.weight']\n",
      "- This IS expected if you are initializing RobertaForMaskedLM from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing RobertaForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'score': 0.1671387106180191, 'token': 30412, 'token_str': ' mathematical', 'sequence': 'this course will teach you all about mathematical models.'}, {'score': 0.03997687250375748, 'token': 38163, 'token_str': ' computational', 'sequence': 'this course will teach you all about computational models.'}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "unmasker = pipeline(\"fill-mask\")\n",
    "results = unmasker(\"this course will teach you all about <mask> models.\", top_k = 2)\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4bdb96f",
   "metadata": {},
   "source": [
    "## 1.5 命名实体识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "14487942",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to dbmdz/bert-large-cased-finetuned-conll03-english and revision 4c53496 (https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n",
      "Some weights of the model checkpoint at dbmdz/bert-large-cased-finetuned-conll03-english were not used when initializing BertForTokenClassification: ['bert.pooler.dense.bias', 'bert.pooler.dense.weight']\n",
      "- This IS expected if you are initializing BertForTokenClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing BertForTokenClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
      "Device set to use cpu\n",
      "c:\\develop\\python_code\\llm_algorithm\\.venv\\Lib\\site-packages\\transformers\\pipelines\\token_classification.py:186: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy=\"AggregationStrategy.SIMPLE\"` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'entity_group': 'PER', 'score': np.float32(0.9846433), 'word': 'Liuzw', 'start': 11, 'end': 16}, {'entity_group': 'ORG', 'score': np.float32(0.99144715), 'word': 'Hugging Face', 'start': 31, 'end': 43}, {'entity_group': 'LOC', 'score': np.float32(0.9994752), 'word': 'Beijing', 'start': 47, 'end': 54}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "ner = pipeline(\"ner\", grouped_entities=True)\n",
    "results = ner(\"My name is Liuzw and I work at Hugging Face in Beijing.\")\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80088c4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-1.5607,  1.6123],\n",
      "        [ 4.1692, -3.3464]], grad_fn=<AddmmBackward0>)\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
    "\n",
    "checkpoint = \"distilbert-base-uncased-finetuned-sst-2-english\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(checkpoint)\n",
    "\n",
    "model = AutoModelForSequenceClassification.from_pretrained(checkpoint)\n",
    "\n",
    "raw_inputs = [\n",
    "    \"I've been waiting for a HuggingFace course my whole life.\",\n",
    "    \"I hate this so much!\",\n",
    "]\n",
    "\n",
    "inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors = \"pt\")\n",
    "\n",
    "# print(inputs)\n",
    "\n",
    "outputs = model(**inputs)\n",
    "print(outputs.logits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e2cb04f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[4.0195e-02, 9.5980e-01],\n",
      "        [9.9946e-01, 5.4418e-04]], grad_fn=<SoftmaxBackward0>)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n",
    "print(predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c9f0ecb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 'NEGATIVE', 1: 'POSITIVE'}\n"
     ]
    }
   ],
   "source": [
    "print(model.config.id2label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d088d1dc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm-algorithm (3.13.7)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
